﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports SdlDotNet.Input
Imports SdlDotNet.Audio
Imports System.Drawing

Public Class CharacterSelect
    Implements IScreen

    Private Players(4) As Player

    Private Avatars(4, 2) As Avatar
    Private AvatarSelected(4, 2) As SelectionType
    Private _numberOfAvatarsSelected As Integer = 0
    Private Property NumberOfAvatarsSelected() As Integer
        Get
            Return _numberOfAvatarsSelected
        End Get
        Set(ByVal value As Integer)
            If (_numberOfAvatarsSelected = 5 And value <> 5) Then
                ClickToStartSprite.Surface = AwaitingSelectionsSurface
            ElseIf (_numberOfAvatarsSelected <> 5 And value = 5) Then
                ClickToStartSprite.Surface = SelectionsCompleteSurface
            End If
            _numberOfAvatarsSelected = value
        End Set
    End Property
    Private Grid As Sprite
    Private HumanPlayerSelectedSprite As Sprite
    Private ClickToStartSprite As Sprite
    Private ReadOnly AwaitingSelectionsSurface As Surface = New Surface("resources\Images\CharacterSelect\c2-3.jpg")
    Private ReadOnly SelectionsCompleteSurface As Surface = New Surface("resources\Images\CharacterSelect\c2-32.jpg")
    Private ComputerPlayerSelectedSprite As Sprite

    Private SelectedAvatars(4) As Avatar

    Public Event AllAvatarsSelected(ByVal players() As Player)
    Public Event RequestReturnToMainMenu()

    Public Sub New()
        InitializePlayers()
        InitializeAvatars()
        InitializeAvatarSelected()
        InitializeSprites()
    End Sub

    Public Sub Dispose() Implements IScreen.Dispose
        Grid.Dispose()
        HumanPlayerSelectedSprite.Dispose()
        ComputerPlayerSelectedSprite.Dispose()
    End Sub

    Private Sub InitializePlayers()
        For i As Integer = 0 To 4
            Players(i) = New Player(i + 1) With {.HumanControlled = True}
        Next
    End Sub

    Private Sub InitializeAvatarSelected()
        For i As Integer = 0 To 4
            For j As Integer = 0 To 2
                AvatarSelected(i, j) = SelectionType.NotSelected
            Next
        Next
    End Sub

    Private Sub InitializeAvatars()
        Avatars(0, 0) = New Avatar("Bush", "Resources\images\CharacterSelect\c1-1.jpg", New Point(0, 0))
        Avatars(1, 0) = New Avatar("Truman", "Resources\images\CharacterSelect\c1-2.jpg", New Point(160, 0))
        Avatars(2, 0) = New Avatar("Eisenhower", "Resources\images\CharacterSelect\c1-3.jpg", New Point(320, 0))
        Avatars(3, 0) = New Avatar("JFK", "Resources\images\CharacterSelect\c1-4.jpg", New Point(480, 0))
        Avatars(4, 0) = New Avatar("Nixon", "Resources\images\CharacterSelect\c1-5.jpg", New Point(640, 0))

        Avatars(0, 1) = New Avatar("Gorbachev", "Resources\images\CharacterSelect\c2-1.jpg", New Point(0, 200))
        Avatars(1, 1) = New Avatar("Andropov", "Resources\images\CharacterSelect\c2-2.jpg", New Point(160, 200))
        Avatars(2, 1) = New Avatar("notanavatar", "Resources\images\CharacterSelect\c2-3.jpg", New Point(320, 200))
        Avatars(3, 1) = New Avatar("Putin", "Resources\images\CharacterSelect\c2-4.jpg", New Point(480, 200))
        Avatars(4, 1) = New Avatar("Stalin", "Resources\images\CharacterSelect\c2-5.jpg", New Point(640, 200))

        Avatars(0, 2) = New Avatar("Hitler", "Resources\images\CharacterSelect\c3-1.jpg", New Point(0, 400))
        Avatars(1, 2) = New Avatar("Churchill", "Resources\images\CharacterSelect\c3-2.jpg", New Point(160, 400))
        Avatars(2, 2) = New Avatar("Castro", "Resources\images\CharacterSelect\c3-3.jpg", New Point(320, 400))
        Avatars(3, 2) = New Avatar("Osama", "Resources\images\CharacterSelect\c3-4.jpg", New Point(480, 400))
        Avatars(4, 2) = New Avatar("Sadam", "Resources\images\CharacterSelect\c3-5.jpg", New Point(640, 400))
    End Sub

    Private Sub InitializeSprites()
        Grid = New Sprite(New Surface("Resources\images\CharacterSelect\grid.png"), New Point(0, 0))
        HumanPlayerSelectedSprite = New Sprite(New Surface("Resources\images\CharacterSelect\HumanPlayer.png"))
        ComputerPlayerSelectedSprite = New Sprite(New Surface("Resources\images\CharacterSelect\ComputerPlayer.png"))
        ClickToStartSprite = New Sprite(AwaitingSelectionsSurface, New Point(320, 200))
    End Sub

    Public Sub Draw(ByRef display As Surface) Implements IScreen.Draw
        For Each availableAvatar As Avatar In Avatars
            availableAvatar.DrawLarge(display)
        Next

        For i As Integer = 0 To 4
            For j As Integer = 0 To 2
                If (AvatarSelected(i, j) = SelectionType.Human) Then
                    display.Blit(HumanPlayerSelectedSprite, Avatars(i, j).LargeSprite.Position)
                ElseIf (AvatarSelected(i, j) = SelectionType.Computer) Then
                    display.Blit(ComputerPlayerSelectedSprite, Avatars(i, j).LargeSprite.Position)
                End If
            Next
        Next

        display.Blit(ClickToStartSprite)

        display.Blit(Grid)
    End Sub

    Public Sub HandleKeyboardUp(ByVal sender As Object, ByVal e As KeyboardEventArgs) Implements IScreen.HandleKeyboardUp
        If (e.Key = Key.Escape) Then
            RaiseEvent RequestReturnToMainMenu()
        End If
    End Sub

    Public Sub HandleMouseButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Implements IScreen.HandleMouseButtonUp
        Dim mousePosition As New Point(e.X, e.Y)

        If (ClickToStartSprite.IntersectsWith(mousePosition)) Then
            If (NumberOfAvatarsSelected = 5) Then
                AssignAvatarsToPlayers()
                RaiseEvent AllAvatarsSelected(Players)
            End If
        Else
            For i As Integer = 0 To 4
                For j As Integer = 0 To 2
                    If (Avatars(i, j).LargeSprite.IntersectsWith(mousePosition)) Then

                        If (AvatarSelected(i, j) = SelectionType.NotSelected) Then
                            AvatarSelected(i, j) = SelectionType.Human
                            NumberOfAvatarsSelected += 1
                        ElseIf (AvatarSelected(i, j) = SelectionType.Human) Then
                            AvatarSelected(i, j) = SelectionType.Computer
                        Else
                            AvatarSelected(i, j) = SelectionType.NotSelected
                            NumberOfAvatarsSelected -= 1
                        End If

                        If (NumberOfAvatarsSelected = 5) Then
                            Avatars(2, 1) = New Avatar("notanavatar", "resources\Images\CharacterSelect\c2-32.jpg", New Point(320, 200))
                        End If

                        Exit For
                    End If
                Next
            Next
        End If
    End Sub

    Private Sub AssignAvatarsToPlayers()
        Dim currentPlayerIndex As Integer = 0

        For i As Integer = 0 To 4
            For j As Integer = 0 To 2
                If (AvatarSelected(i, j) <> SelectionType.NotSelected) Then
                    Players(currentPlayerIndex).Avatar = Avatars(i, j)
                    If (AvatarSelected(i, j) = SelectionType.Computer) Then
                        Players(currentPlayerIndex).HumanControlled = False
                    Else
                        Players(currentPlayerIndex).HumanControlled = True
                    End If
                    currentPlayerIndex += 1
                End If
            Next
        Next
    End Sub

    Public Sub HandleMouseMotion(ByVal sender As Object, ByVal e As MouseMotionEventArgs) Implements IScreen.HandleMouseMotion
        ' nothing to do
    End Sub

End Class
